// reservation/publish/publish.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import notice from '../../utils/notice.js';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            order: JSON.parse(decodeURIComponent(options.order))
        })
    },

    inputName(e) {
        this.name = e.detail.value;
    },

    updateImg() {
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            success: res => {
                console.log(res.tempFiles[0].tempFilePath)
                network.uploadFile(res.tempFiles[0].tempFilePath, 'file', {}).then(img => {
                    this.setData({
                        images: img
                    })
                })
            }
        })
    },

    inputCount(e) {
        this.peopleNum = e.detail.value;
    },

    handleChange(e) {
        this.setData({
            checked: e.detail.checked,
        });
    },
    publishTap() {
        if (!this.name) {
            wx.showToast({
                icon: 'none',
                title: '请输入活动主题',
            })
            return;
        }
        if (!this.data.images) {
            wx.showToast({
                icon: 'none',
                title: '请上传活动海报',
            })
            return;
        }
        if (!this.peopleNum) {
            wx.showToast({
                icon: 'none',
                title: '请输入报名人数',
            })
            return;
        }
        let user = getApp().globalData.userInfo;
        network.postJson(api.createActive, {
            creator: user.id,
            siteId: this.data.order.siteId,
            orderId: this.data.order.id,
            name: this.name,
            peopleNum: this.peopleNum,
            images: this.data.images,
            mode: 1
        }).then(res => {
            notice.postNotice("RefreshOrder");
            wx.showToast({
                icon: 'none',
                title: '发布成功'
            })
            setTimeout(function () {
                wx.navigateBack();
            }, 2000)
        })
    }
})